package Reflex.获取实例;

import Reflex.类.Student;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class InstancePrivateDemo {
    /**
     * 获取私有方法
     *
     */
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        //获取类
        Class stuClass = Class.forName("Reflex.类.Student");
        //实例化该类
        Student stuInstance = (Student) stuClass.newInstance();
        //获取私有方法  有参数要获取参数类型
        Method stuMethod = stuClass.getDeclaredMethod("privateMethod",String.class);
        /**  暴力破解  打破私有访问界限 */
        stuMethod.setAccessible(true);
        //援引 ， 并且要入参
        Object invoke = stuMethod.invoke(stuInstance,"Rose");
        //无返回值 ， 返回null
        System.out.println(invoke);
    }
}
